/* License Notice:
**
** This program is free software: you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation, either version 3 of the License, or
**    (at your option) any later version.
** This program is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
**   GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
**   along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
 * @file keyboard.cpp
 * @author TooOld2Rock'nRoll
 * @see keyboard.hpp
 */
/*---- Includes ----*/
#include "input_devices/keyboard.hpp"

#include "toolbox/debug.h"


/*---- Global Variables ----*/


/*---- Methods Implementation ----*/
/**
 * @brief Base Constructor.
 */
Keyboard::Keyboard () : InputDevice ()
{
    _debug (TRACE, "::%s ()", __FUNCTION__);

    //get pointer to the entire keyboard state and the total number of keys presente
    //it must be updated with SDL_PumpEvents() before reading
    this->ui8a_sdl_keyboard_state = SDL_GetKeyboardState (&(int &)this->u_key_count);

    this->ui8a_save_keyboard_state = new Uint8[this->u_key_count] ();

    this->a_keyboard_state = new key_state[this->u_key_count] ();
    std::memset ((void *)this->a_keyboard_state, key_state::_key_state_max, this->u_key_count * sizeof(key_state));
}//End Constructor

/**
 * @brief Destructor.
 */
Keyboard::~Keyboard ()
{
    _debug (TRACE, "::%s ()", __FUNCTION__);

    if (this->ui8a_save_keyboard_state)
        delete[] this->ui8a_save_keyboard_state;

    if (this->a_keyboard_state)
        delete[] this->a_keyboard_state;
}//End Destructor

/**
 * @brief Updates the imediate keyboard state.
 */
void Keyboard::update ()
{
    int i = 0;

    _debug (TRACE, "::%s ()", __FUNCTION__);

    //update the OS layer
    //SDL_PumpEvents ();//update the keyboard keys state, it is necessary, but will be called on the EventManager!
    this->_key_modifiers = SDL_GetModState ();//get state of keys like Shift, Ctrl, Alt, etc...

    //update our array of keys
    std::memset ((void *)this->a_keyboard_state, key_state::_key_state_max, this->u_key_count * sizeof(key_state));
    for (i = this->u_key_count; i--; )
    {
        //check if key is unpressed, most common state
        if (!this->ui8a_sdl_keyboard_state[i] && !this->ui8a_save_keyboard_state[i])
            continue;
        //check if key was pressed and still is
        else if (this->ui8a_sdl_keyboard_state[i] && this->ui8a_save_keyboard_state[i])
            this->a_keyboard_state[i] = key_state::HOLD;
        //check if key was just released
        else if (!this->ui8a_sdl_keyboard_state[i] && this->ui8a_save_keyboard_state[i])
            this->a_keyboard_state[i] = key_state::UP;
        //check if key was just pressed
        else if (this->ui8a_sdl_keyboard_state[i] && !this->ui8a_save_keyboard_state[i])
            this->a_keyboard_state[i] = key_state::DOWN;
    }//end for (i = this->u_key_count; i--; )

    //save previous state
    std::memcpy ((void *)this->ui8a_save_keyboard_state, (void *)this->ui8a_sdl_keyboard_state, this->u_key_count * sizeof(Uint8));
}//End update ()

/**
 * @brief Get the desired key state since last update.
 *
 * @param scancode - the key scancode.
 *
 * @return The desired key state.
 *
 * @throws std::out_of_range - if the tile index is greater than the max number of keys supported.
 */
key_state Keyboard::getKeyState (SDL_Scancode scancode) const
{
    _debug (TRACE, "::%s (%u)", __FUNCTION__, scancode);

    if (scancode >= this->u_key_count)
        throw std::out_of_range (fmt::format ("{0}:{1} - Key out of range, {2} of {3}!", __LINE__, __FILE__, scancode, this->u_key_count - 1));

    return this->a_keyboard_state[scancode];
}//End getKeyState ()

